﻿/*
 * Name:        BaseEntity class
 * Description: A base entity to inhert all other entities from
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

using Gyvate.Util;
using Gyvate.Worldspawn;

namespace Gyvate.Entities
{
    internal class BaseEntity
    {
        protected Vector2d m_pos;
        protected Image m_spriteImage;
        protected Bitmap m_sprite;
        protected TileList m_tile;

        protected Game m_game;

        public TileList GetTileType() { return m_tile; }    //Returns the tile type

        /*
         * Constructor
         */
        public BaseEntity(Vector2d myPos, Game game)
        {
            m_pos = myPos;
            m_game = game;
            m_tile = TileList.TILE_NONE;
        }

        /*
         * Default collision action
         */
        public virtual void Collision() { }

        /*
         * Draws entity to screen
         * toConsole - development tool to print it to console instead of drawing
         */
        internal virtual void Draw(bool toConsole = false)
        {
            if (toConsole)
            {
                Console.SetCursorPosition(m_pos.x + 1, m_pos.y + 1);
                Console.Write('&');
            }

            if (m_sprite == null)
            {
                Console.Error.WriteLine("Missing sprite.");
                return;
            }
            if (m_game.GetGraphics() == null)
                return;

            m_game.GetGraphics().DrawImage(m_sprite, new Point(m_pos.x * m_spriteImage.Width, m_pos.y * m_spriteImage.Height));
        }

        /*
         * Loads the sprite
         */
        public void SetSprite(string sz)
        {
            PrecachedSprite spr = m_game.GetPrecachedSprite(sz);

            if (spr == null)
                return;

            m_spriteImage = spr.GetImage();
            m_sprite = spr.GetBitmap();
        }

        /*
         * Gets current position of the entity
         */
        internal Vector2d GetPos() { return m_pos; }
    }
}
